package com.mobor.moborsvr.front.category.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mobor.moborsvr.front.category.constant.MoborFrontMessages;
import com.mobor.moborsvr.obj.category.categories.CategoryObjectDO;
import com.mobor.moborsvr.obj.category.service.CategoryObjectService;

/**
 * Main object categories controller which gives possibility to manipulate (CRUD) object categories using simple RESTful API
 * @author teroleg
 *  
 */
@Controller
public class CategoryObjectController {
	
	private static final Logger logger = LogManager.getLogger(CategoryObjectController.class.getName());
	
	@Autowired
	private CategoryObjectService categoryObjectService;
	    
    /**
     * This method finds and returns all existing object categories arranged into the list 
     * @param request
     * @param response
     * @return list of all existing object categories
     * @throws Throwable
     */
	@RequestMapping(value="/categories", method = RequestMethod.GET)
    protected @ResponseBody List<CategoryObjectDO> getAllCategories(HttpServletRequest request, HttpServletResponse response){
		
		logger.info(MoborFrontMessages.FRONT_I_OBJECT_CATEGORIES_SEARCH);
		
		List<CategoryObjectDO> coList = categoryObjectService.getObjectCategories();

        return coList;
    }
	/**
	 * Finds object by provided id
	 * @param coId id
	 * @param request
	 * @param response
	 * @return found object with id or null
	 */
	@RequestMapping(value="/categories/{coId}", method = RequestMethod.GET)
    protected @ResponseBody CategoryObjectDO getObjectCategoryById(@PathVariable Long coId, HttpServletRequest request, HttpServletResponse response){
			
		logger.info(MoborFrontMessages.FRONT_I_OBJECT_CATEGORY_SEARCH + coId);
		
		CategoryObjectDO co = categoryObjectService.getObjectCategory(coId);

        return co;
	}
}
